package com.theobserver.theobserver;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.GetCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wssxk on 7/11/17.
 */

public class ColumnDetailActivity extends Activity {

    AVUser cur_user = null;
    AVObject cur_column = null;
    AVObject subscrib_instance = null;
    int is_star = -1;

    public void onCreate(Bundle savedInstance)
    {
        super.onCreate(savedInstance);

        setContentView(R.layout.news_list_without_catalog_independ_page);

        View add_more = findViewById(R.id.relativelayout_news_addmore);
        add_more.setVisibility(View.GONE);

        ListView lv = findViewById(R.id.listview_news);
        final NewsListAdapter adapter = new NewsListAdapter(ColumnDetailActivity.this);
        lv.setAdapter(adapter);

        final SwipeRefreshLayout sl = findViewById(R.id.swiperefreshlayout_news);
        sl.setRefreshing(true);

        TextView textview_return = findViewById(R.id.textview_newslist_independ_return);
        textview_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        Intent intent = getIntent();
        cur_column = AVObject.createWithoutData("column", intent.getStringExtra("objectId"));
        cur_column.fetchInBackground(new GetCallback<AVObject>() {
            @Override
            public void done(AVObject avObject, AVException e) {
                if(e != null)
                {
                    sl.setRefreshing(false);
                    //Toast.makeText(ColumnDetailActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    //Toast.makeText(ColumnDetailActivity.this, "获取ing", Toast.LENGTH_SHORT).show();
                    cur_column = avObject;
                    News.getColumnNews(cur_column, new NewsCallBack() {
                        @Override
                        public void getNews(ArrayList<News> news, int state, AVException e) {

                            sl.setRefreshing(false);
                            //Toast.makeText(ColumnDetailActivity.this, "获取ing", Toast.LENGTH_SHORT).show();
                            if(news != null)
                            {
                                adapter.data.clear();
                                adapter.data.addAll(news);
                                adapter.notifyDataSetChanged();
                                //Toast.makeText(ColumnDetailActivity.this, "获取成功", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            }
        });


        sl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                News.getColumnNews(cur_column, new NewsCallBack() {
                    @Override
                    public void getNews(ArrayList<News> news, int state, AVException e) {

                        sl.setRefreshing(false);
                        //Toast.makeText(ColumnDetailActivity.this, "获取ing", Toast.LENGTH_SHORT).show();
                        if(news != null)
                        {
                            adapter.data.clear();
                            adapter.data.addAll(news);
                            adapter.notifyDataSetChanged();
                            //Toast.makeText(ColumnDetailActivity.this, "获取成功", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent news_detail_intent = new Intent(ColumnDetailActivity.this, NewsDetailActivity.class);
                //news_detail_intent.putExtras();
                news_detail_intent.putExtra("objectId",
                        ((NewsItemFrameLayout) view.findViewById(R.id.framelayout_news_item)).news.cur_news.getObjectId());
                startActivity(news_detail_intent);
            }
        });



        //用户订阅相关-----------------需要修改

        final ImageView imageview_star = (ImageView) findViewById(R.id.imageview_column_detail_star);

        //cur_user = (AVUser) AVUser.createWithoutData("_User", "5961b956ac502e005819946c");    //用这种方法回闪退

        cur_user = AVUser.getCurrentUser();

        //cur_user = new AVUser();
        //cur_user.setObjectId("5961b956ac502e005819946c");

        if(cur_user != null)
        {
            AVQuery<AVObject> column_star_query = new AVQuery<>("column_subscrib");
            column_star_query.whereEqualTo("user_email", cur_user);
            column_star_query.whereEqualTo("column_id", cur_column);
            column_star_query.findInBackground(new FindCallback<AVObject>() {
                @Override
                public void done(List<AVObject> list, AVException e) {
                    if(list != null && list.size() != 0)
                    {
                        subscrib_instance = list.get(0);
                        imageview_star.setImageResource(R.drawable.ic_action_star_red);
                        is_star = 1;
                    }
                    else
                    {
                        is_star = 0;
                    }

                    imageview_star.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if(is_star == 0)
                            {
                                AVObject t_subscrib = new AVObject("column_subscrib");
                                t_subscrib.put("user_email", cur_user);
                                t_subscrib.put("column_id", cur_column);
                                t_subscrib.saveInBackground();
                                subscrib_instance = t_subscrib;
                                is_star = 1;
                                imageview_star.setImageResource(R.drawable.ic_action_star_red);
                                Toast.makeText(ColumnDetailActivity.this, "订阅成功", Toast.LENGTH_SHORT).show();
                            }
                            else if(is_star == 1)
                            {
                                subscrib_instance.deleteInBackground();
                                is_star = 0;
                                imageview_star.setImageResource(R.drawable.ic_action_start_white);
                                Toast.makeText(ColumnDetailActivity.this, "取消订阅", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            });
        }
        else
        {
            imageview_star.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(ColumnDetailActivity.this, "请先登陆", Toast.LENGTH_SHORT).show();
                }
            });
        }

        /*
        cur_user.fetchInBackground(new GetCallback<AVObject>() {
            @Override
            public void done(AVObject avObject, AVException e) {
                if(e == null)
                {
                    AVQuery<AVObject> column_star_query = new AVQuery<AVObject>("column_subscrib");
                    column_star_query.whereEqualTo("user_email", cur_user);
                    column_star_query.findInBackground(new FindCallback<AVObject>() {
                        @Override
                        public void done(List<AVObject> list, AVException e) {
                            if(list != null && list.size() != 0)
                            {
                                subscrib_instance = list.get(0);
                                imageview_star.setImageResource(R.drawable.ic_action_star_red);
                                is_star = 1;
                            }
                            else
                            {
                                is_star = 0;
                            }

                            imageview_star.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    if(is_star == 0)
                                    {
                                        AVObject t_subscrib = new AVObject("column_subscrib");
                                        t_subscrib.put("user_email", cur_user);
                                        t_subscrib.put("column_id", cur_column);
                                        t_subscrib.saveInBackground();
                                        subscrib_instance = t_subscrib;
                                        is_star = 1;
                                        imageview_star.setImageResource(R.drawable.ic_action_star_red);
                                        Toast.makeText(ColumnDetailActivity.this, "订阅成功", Toast.LENGTH_SHORT).show();
                                    }
                                    else if(is_star == 1)
                                    {
                                        subscrib_instance.deleteInBackground();
                                        is_star = 0;
                                        imageview_star.setImageResource(R.drawable.ic_action_start_white);
                                        Toast.makeText(ColumnDetailActivity.this, "取消订阅", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }
                    });
                }
            }
        });
        */







    }

}
